/*
 * @Author: ZhanXie
 * @Date: 2020-09-03 17:13:02
 * @LastEditors: ZhanXie
 * @LastEditTime: 2020-09-04 11:32:18
 * @Description: utils api 路由
 * @FilePath: /weibo/src/routes/api/utils.js
 */
const router = require('koa-router')()
const koaForm = require('formidable-upload-koa')
const { saveFile } = require('../../controller/utils')
const { loginCheck } = require('../../middlewares/loginChecks')

router.prefix('/api/utils')

// 上传图片
router.post('/upload', loginCheck, koaForm(), async (ctx, next) => {
  const file = ctx.req.files['file']
  if (!file) {
    return
  }
  const { size, path, name, type } = file
  ctx.body = await saveFile({
    filePath: path,
    size,
    name,
    type
  })
})

module.exports = router